Source Code Viewer - Example 3 - AbstractInMemoryIndex

package name.matthewgreet.example3.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;

/**
 * Retrieves a sublist using a distinct key from an instance of {@link AbstractInMemoryTable} as if retrieving from a 
 * database table using an index.  This exists as part of a demonstration in place of actual database tables.
 * 
 * @param <K> Type of key.
 * @param <T> Class of Data Transfer Object describing a record.
 */
public abstract class AbstractInMemoryIndex<K extends Comparable<K>,PK extends Comparable<PK>,T extends AbstractRecord<PK>> {
	private SortedMap<K,List<T>> table;
	
	public AbstractInMemoryIndex(Collection<T> initialData, Function<T,K> keyExtractor) {
		super();
		
		table = new TreeMap<>();
		for (T record: initialData) {
			K key = keyExtractor.apply(record);
			if (key != null) {
				List<T> sublist = table.get(key);
				if (sublist == null) {
					sublist = new ArrayList<>();
					table.put(key, sublist);
				}
				sublist.add(record);
			}
		}
		for (Entry<K, List<T>> entry: table.entrySet()) {
			Collections.sort(entry.getValue(), (r1,r2) -> r1.getPrimaryKey().compareTo(r2.getPrimaryKey()));
		}
	}
	
	public List<T> findByKey(K key) {
		List<T> result;
		result = table.get(key);
		if (result == null) {
			result = new ArrayList<>();
		}
		return result;
	}
	
	/**
	 * Keys are inclusive. 
	 */
	public List<T> findByKeyRange(K startKey, K endKey) {
		List<T> result;
		SortedMap<K,List<T>> submap;
		
		result = new ArrayList<>();
		submap = table.subMap(startKey, endKey);
		for (List<T> list: submap.values()) {
			result.addAll(list);
		}
		result.addAll(table.get(endKey));
		return result;
	}
	
	/**
	 * Indices are inclusive.
	 */
	public List<T> findRangeByKey(K key, int startIndex, int endIndex) {
		List<T> list;
		int workingStartIndex, workingEndIndex;
		
		list = table.get(key);
		
		if (list != null) {
			workingStartIndex = startIndex;
			if (workingStartIndex > list.size()) {
				workingStartIndex = list.size();
			}
			workingEndIndex = endIndex + 1;
			if (workingStartIndex > list.size()) {
				workingStartIndex = list.size();
			}
			return list.subList(workingStartIndex, workingEndIndex);
		} else {
			return new ArrayList<>();
		}
	}
	
	/**
	 * Indices are inclusive.
	 */
	public List<T> findRangeByKeyRange(K startKey, K endKey, int startIndex, int endIndex) {
		List<T> sublist, endKeyList;
		SortedMap<K,List<T>> submap;
		int workingStartIndex, workingEndIndex;
		
		
		sublist = new ArrayList<>();
		submap = table.subMap(startKey, endKey);
		for (List<T> list: submap.values()) {
			sublist.addAll(list);
		}
		endKeyList = table.get(endKey);
		if (endKeyList != null) {
			sublist.addAll(endKeyList);
		}
		
		workingStartIndex = startIndex;
		if (workingStartIndex > sublist.size()) {
			workingStartIndex = sublist.size();
		}
		workingEndIndex = endIndex + 1;
		if (workingStartIndex > sublist.size()) {
			workingStartIndex = sublist.size();
		}
		return sublist.subList(workingStartIndex, workingEndIndex);
	}
	
	public int getListSizeByKey(K key) {
		List<T> list;
		
		list = table.get(key);
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}
	
	public int getListSizeByKeyRange(K startKey, K endKey) {
		SortedMap<K,List<T>> submap;
		int result;

		result = 0;
		submap = table.subMap(startKey, endKey);
		for (List<T> list: submap.values()) {
			result = result + list.size();
		}
		return result;
	}
}